﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ChatServer
{
    class Connection
    {
        TcpClient tcpClient;

        //deze thread stuurt info naar de client
        private Thread Sender;

        //vangt messages af
        private StreamReader Recieve;

        //vangt user af
        private StreamWriter From;

        //username + message
        private String User;
        private String Response;

        //thread die connecties accepteert en op messages wacht
        public Connection(TcpClient tcpConnection)
        {
            tcpClient = tcpConnection;
            Sender = new Thread(AcceptClient);
            Sender.Start();
        }

        //sluit connecties
        public void CloseConnection()
        {
            tcpClient.Close();
            Recieve.Close();
            From.Close();
            Sender.Abort();
        }

        //run deze methode als een nieuwe client connect
        private void AcceptClient()
        {
            Recieve = new StreamReader(tcpClient.GetStream());
            From = new StreamWriter(tcpClient.GetStream());

            //account informatie uitlezen
            User = Recieve.ReadLine();

            //response van client afvangen
            if (User != "")
            {
                //checken of gebruikersnaam al bestaat
                if (Monitor.UserTable.Contains(User) == true)
                {
                    //0 == not connected
                    From.WriteLine("0|This username already exists");
                    From.Flush();
                    CloseConnection();
                    return;
                }
                //checken of gebruikersnaam niet "Administrator" is
                else if (User == "Administrator" || User == "Admin" || User == "administrator" || User == "admin")
                {
                    //0 == not connected
                    From.WriteLine("0|This username is reserved");
                    From.Flush();
                    CloseConnection();
                    return;
                }
                //als aan bovenstaande criteria voldaan word dan kan de user connecten
                else
                {
                    //1 betekent connected
                    From.WriteLine("1");
                    From.Flush();

                    //user in hashtable stoppen en beginnen met luisteren naar berichten
                    Monitor.AddUser(tcpClient, User);
                }
            }
            //als de username leeg is
            else
            {
                CloseConnection();
                return;
            }

            try
            {
                //wachten op message van user
                while ((Response = Recieve.ReadLine()) != "")
                {
                    //als het niet valid is, remove gebruiker
                    if (Response == null)
                    {
                        Monitor.RemoveUser(tcpClient);
                    }
                    else
                    {
                        //verstuur bericht naar iedereen
                        Monitor.SendMessage(User, Response);
                    }
                }
            }
            catch
            {
                //als er iets fout gaat disconnect user
                Monitor.RemoveUser(tcpClient);
            }
        }
    }
}
